package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Brand;
import com.ssh.entity.Company;
import com.ssh.entity.Product;
import com.ssh.entity.SuperMarket;
import com.ssh.entity.SuperMarketProduct;
import com.ssh.manager.BrandManager;
import com.ssh.manager.CompanyManager;
import com.ssh.manager.ProductManager;
import com.ssh.manager.SuperMarketManager;
import com.ssh.manager.SuperMarketProductManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/marketProduct/marketProduct.jsp"),
@Result(name="findMarketProByPage", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.storecode,page\\.pageData\\[\\d+\\]\\.price,page\\.pageData\\[\\d+\\]\\.createDate," +
"page\\.pageData\\[\\d+\\]\\.product\\.id,page\\.pageData\\[\\d+\\]\\.product\\.name,page\\.pageData\\[\\d+\\]\\.superMarket\\.id," +
"page\\.pageData\\[\\d+\\]\\.superMarket\\.name,page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.id,page\\.pageData\\[\\d+\\]\\.superMarket\\.company\\.name," +
"page\\.pageData\\[\\d+\\]\\.product\\.brand\\.id,page\\.pageData\\[\\d+\\]\\.product\\.brand\\.name"}),
@Result(name="updateMarketProduct", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="deleteMarketProduct", type="json", params={"root","page","excludeproperties","page\\.curPageNum"})
})
public class MarketProductAction extends ActionSupport{
	@Resource(name="superMarketManagerImpl")
	private SuperMarketManager superMarketManager;
	@Resource(name="companyManagerImpl")
	private CompanyManager companyManager;
	@Resource(name="productManagerImpl")
	private ProductManager productManager;
	@Resource(name="superMarketProductManagerImpl")
	private SuperMarketProductManager superMarketProductManager;
	@Resource(name="brandManagerImpl")
	private BrandManager brandManager;
	
	private JsonResult jr=new JsonResult();
	private SuperMarket superMarket;
	private Brand brand;
	private Company company;
	private Product product;
	private SuperMarketProduct superMarketProduct;
	private Page page;
	private String isDesc;
	private String ids;
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public Company getCompany() {
		return company;
	}
	public void setCompany(Company company) {
		this.company = company;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public SuperMarket getSuperMarket() {
		return superMarket;
	}
	public void setSuperMarket(SuperMarket superMarket) {
		this.superMarket = superMarket;
	}
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	public SuperMarketProduct getSuperMarketProduct() {
		return superMarketProduct;
	}
	public void setSuperMarketProduct(SuperMarketProduct superMarketProduct) {
		this.superMarketProduct = superMarketProduct;
	}
	
	/**
	 * 前往jsp页面
	 * @return
	 */
	public String goMarketProductJsp(){
		
		return "success";
	}
	
	/**
	 * 按条件分页查询门店商品
	 * @return
	 */
	public String findMarketProByPage(){
		page.setNumberPerPage(20);
		Map conditionMap = new HashMap();
		//conditionMap.put("companyId", company.getId());
		//conditionMap.put("brandId", brand.getId());
		conditionMap.put("marketId", superMarket.getId());
		conditionMap.put("productName", product.getName());
		superMarketProductManager.findMarketProductListByPage(conditionMap, page, isDesc);
		return "findMarketProByPage";
	}
	
	/**
	 * 保存门店商品
	 * @return
	 */
	public String saveMarketProduct(){
		product = productManager.getProductById(product.getId());
		superMarket = superMarketManager.getSuperMarketById(superMarket.getId());
		
		superMarketProduct.setProduct(product);
		superMarketProduct.setSuperMarket(superMarket);
		superMarketProduct.setCreateDate(new Timestamp(System.currentTimeMillis()));
		superMarketProductManager.saveOrUpdateEntity(superMarketProduct);
		return "success";
	}
	
	/**
	 * 修改门店商品
	 * @return
	 */
	public String updateMarketProduct(){
		try {
			SuperMarketProduct sMarketProduct = superMarketProductManager.getSuperMarketProductById(superMarketProduct.getId());
			
			product = productManager.getProductById(product.getId());
			superMarket = superMarketManager.getSuperMarketById(superMarket.getId());
			
			sMarketProduct.setStorecode(superMarketProduct.getStorecode());
			sMarketProduct.setPrice(superMarketProduct.getPrice());
			sMarketProduct.setProduct(product);
			sMarketProduct.setSuperMarket(superMarket);
			
			superMarketProductManager.saveOrUpdateEntity(sMarketProduct);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("门店商品修改失败", LogName.API);
			e.printStackTrace();
		}
		return "updateMarketProduct";
	}
	
	/**
	 * 删除门店商品
	 * @return
	 */
	public String deleteMarketProduct(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					superMarketProduct = superMarketProductManager.getSuperMarketProductById(new Long(d[i]));
					superMarketProduct.setDel(1);
					superMarketProductManager.saveOrUpdateEntity(superMarketProduct);
				}
			}else{
				superMarketProduct = superMarketProductManager.getSuperMarketProductById(superMarketProduct.getId());
				superMarketProduct.setDel(1);
				superMarketProductManager.saveOrUpdateEntity(superMarketProduct);
			}
			
			page.setNumberPerPage(20);
			int _p = (int) page.getCurPageNum();
			Map conditionMap = new HashMap();
			conditionMap.put("marketId", superMarket.getId());
			conditionMap.put("productName", product.getName());
			page.setCurPageNum(1);
			superMarketProductManager.findMarketProductListByPage(conditionMap, page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("门店商品删除失败", LogName.API);
			e.printStackTrace();
		}
		return "deleteMarketProduct";
	}
}
